﻿#include <fstream>
#include <iomanip>
#include "LineLDParCheck.h"
#include <BPAFilesIO.h>
#include <BaseCommon.h>

using namespace JGG;

LineLDParCheck::LineLDParCheck(BPA::BPAFilesIO::DatParse* datParse, const char* database, int caseid)
{
    this->datParse = datParse;
    this->database = database;
    this->caseid = caseid;

    inst = new BPA::BPAFilesIO::GetDatFromDb(datParse, database, caseid);
}

LineLDParCheck::~LineLDParCheck()
{
    if (inst)
    {
        delete inst;
        inst = nullptr;
    }
}

void LineLDParCheck::LineLdPviMatch()
{
	LINEVector LineLDListc = inst->GetLineLDList();
	BUSVector BusListc = inst->GetBusList();

    BPA::BPAFilesIO::GetInitConfigFromDb Inf(database);
	vector <double> Parameterc;
	Parameterc = Inf.GetParameter();
	int size = LineLDListc.size();

	for (int i = 0; i < size; i++)
	{
		if ((LineLDListc[i].Pdc == 999999) && (LineLDListc[i].Vdc != 999999))
		{
			LineLDListc[i].LinePro = "缺少给定整流测直流电压";
			LineLdInfUncomplet.push_back(LineLDListc[i]);
			/*std::cout << "ERROR:" << "  " << BusListc[LineLDListc[i].StartNum - 1].BusName << "  " << LineLDListc[i].StartVol << "  " << BusListc[LineLDListc[i].EndNum - 1].BusName
				<< "  " << LineLDListc[i].EndVol << "  " << "缺少给定侧直流电压" << std::endl;*/
		}
		else if ((LineLDListc[i].Pdc != 999999) && (LineLDListc[i].Vdc == 999999))
		{
			LineLDListc[i].LinePro = "缺少安排直流功率";
			LineLdInfUncomplet.push_back(LineLDListc[i]);
			//cout << "ERROR:" << "  " << BusListc[LineLDListc[i].StartNum - 1].BusName << "  " << LineLDListc[i].StartVol << "  " << BusListc[LineLDListc[i].EndNum - 1].BusName
			//<< "  " << LineLDListc[i].EndVol << "  " << "缺少安排直流功率" << endl;
		}
		else if ((LineLDListc[i].Pdc == 999999) && (LineLDListc[i].Vdc == 999999))
		{
			LineLDListc[i].LinePro = "缺少安排直流功率和给定侧直流电压";
			LineLdInfUncomplet.push_back(LineLDListc[i]);
			//cout << "ERROR:" << "  " << BusListc[LineLDListc[i].StartNum - 1].BusName << "  " << LineLDListc[i].StartVol << "  " << BusListc[LineLDListc[i].EndNum - 1].BusName
			//<< "  " << LineLDListc[i].EndVol << "  " << "缺少安排直流功率和给定侧直流电压" << endl;
		}
		else if ((LineLDListc[i].Pdc != 999999) && (LineLDListc[i].Vdc != 999999))
		{
			if (LineLDListc[i].Idc == 999999)
			{
				//LineLDListc[i].Idc = LineLDListc[i].Pdc / LineLDListc[i].Vdc;
			}
			else if (LineLDListc[i].Idc != 999999)
			{
		
			}
		}
	}
	LineLDListc.clear();
	BusListc.clear();
	Parameterc.clear();
}
void LineLDParCheck::LineLdParCheck()
{
	LINEVector LineLDListc = inst->GetLineLDList();
	string FileName = inst->GetFileName();
    BPA::BPAFilesIO::GetInitConfigFromDb Inf(database);
	vector <double> Parameterc;

	Parameterc = Inf.GetParameter();
	int size;
	//int CaseIDtmp = 0;
	double Parameter1, Parameter2;
	Parameter1 = Parameterc[5];
	Parameter2 = Parameterc[6];
	size = LineLDListc.size();
	//SqliteHelperQuery query = db.execQuery("select * from FileList;");
	/*while (!query.eof())
	{
		if (query.getStringField(0) == FileName)
		{
			CaseIDtmp = query.getIntField(2);
		}
		query.nextRow();
	}
	bufSQL.format("select max(Rdc),min(Rdc) from LineDCList where CaseID <> (%d)",CaseIDtmp);
	SqliteHelperQuery query1 = db.execQuery(bufSQL);
	if (!query1.eof())
	{
		Parameter2 = query1.getFloatField(0);
		Parameter1 = query1.getFloatField(1);
	}*/
	for (int i = 0; i < size; i++)
	{
		if ((LineLDListc[i].Rdc == 999999) || (LineLDListc[i].Rdc == 0))
		{
			LineLDListc[i].LinePro = "直流线路的电阻参数不存在";
			LineLdInfUncomplet.push_back(LineLDListc[i]);
			//cout << "ERROR:" << "  " << BusListc[LineLDListc[i].StartNum - 1].BusName << "  " << LineLDListc[i].StartVol << "  " << BusListc[LineLDListc[i].EndNum - 1].BusName
			//<< "  " << LineLDListc[i].EndVol << "  " << "缺少直流线路的电阻值" << endl;
		}
		else if ((LineLDListc[i].Rdc < Parameter1))
		{
			LineLDListc[i].LinePro = "直流线路的电阻参数可能不合理,电阻值小于历史最小值";
			LineLdProblem.push_back(LineLDListc[i]);
			//cout << "WARNING:" << "  " << BusListc[LineLDListc[i].StartNum - 1].BusName << "  " << LineLDListc[i].StartVol << "  " << BusListc[LineLDListc[i].EndNum - 1].BusName
			//<< "  " << LineLDListc[i].EndVol << "  " << "直流线路的电阻参数可能不合理" << endl;
		}
		else if (LineLDListc[i].Rdc > Parameter2)
		{
			LineLDListc[i].LinePro = "直流线路的电阻参数可能不合理，电阻值大于历史最大值";
			LineLdProblem.push_back(LineLDListc[i]);
		}
	}
	//db.close();
	LineLDListc.clear();
	Parameterc.clear();
}
void LineLDParCheck::LineLd2Dat()
{
	BUSVector BusListc = inst->GetBusList();
    string FileName = inst->GetFileName();
	int size = LineLdInfUncomplet.size();
	int sizec = LineLdProblem.size();
	std::ofstream FunctionResult((FileName + "- ProblemLdLine" + ".dat").c_str(), ios::out);
	setiosflags(ios::right);
	FunctionResult << "直流线路参数可能存在问题线路列表：" << endl << endl;
	FunctionResult << "始端母线" << "  " << setw(8) << "始端电压" << "  " << setw(8) << "末端母线" << "  " << setw(8) << "末端电压" << "  " << setw(8) << "线路电阻" << "  " << setw(8) << "直流功率" <<
		"   " << setw(8) << "直流安排电压" << "  " << setw(8)<< "直流电流" << "  " << setw(8) << "线路长度" << "      " << "线路问题";
	FunctionResult << endl;
	for (int i = 0; i < sizec; i++)
	{
		FunctionResult << LineLdProblem[i].LineStart << "  " << setw(8) << LineLdProblem[i].StartVol << "  " << setw(8) << LineLdProblem[i].LineEnd << "  " << setw(8) << LineLdProblem[i].EndVol << "  " << setw(8) << LineLdProblem[i].Rdc << "  " << setw(8) << LineLdProblem[i].Pdc <<
			"   " << setw(8) << LineLdProblem[i].Vdc << "  " << setw(8) << "  " << LineLdProblem[i].Idc << "  " << setw(8) << LineLdProblem[i].linelength << "      " << LineLdProblem[i].LinePro;
		FunctionResult << endl;
	}
	FunctionResult << "直流线路参数填写不全的线路列表：" << endl << endl;
	FunctionResult << "始端母线" << "  " << setw(8) << "始端电压" << "  " << setw(8) << "末端母线" << "  " << setw(8) << "末端电压" << "  " << setw(8) << "线路电阻" << "  " << setw(8) << "直流功率" <<
		"   " << setw(8) << "直流安排电压" << "  " << setw(8) << "直流电流" << "  " << setw(8) << "线路长度" << "      " << "线路问题";
	FunctionResult << endl;
	for (int i = 0; i < size; i++)
	{
		FunctionResult <<LineLdInfUncomplet[i].LineStart << "  " << setw(8) << LineLdInfUncomplet[i].LineEnd << "  " << setw(8) << LineLdInfUncomplet[i].StartVol << "  " << setw(8) << LineLdInfUncomplet[i].Rdc << "  " << setw(8) << LineLdInfUncomplet[i].Pdc <<
			"   " << setw(8) << LineLdInfUncomplet[i].Vdc << "  " << setw(8) << "  " << LineLdInfUncomplet[i].Idc << "  " << setw(8) << LineLdInfUncomplet[i].linelength << "      " << LineLdInfUncomplet[i].LinePro;
		FunctionResult << endl;
	}
	FunctionResult.close();
	BusListc.clear();
}
void LineLDParCheck::LineLd2Db()
{
	//const char* DB_NAME = "./PowerFlow.db";
	int LineLdInfUncompletNum, LineLdProblemNum;
	LineLdInfUncompletNum = LineLdInfUncomplet.size();
	LineLdProblemNum = LineLdProblem.size();
	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	db.open(database);
	char buf[500];
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  LineLdInfUncompletList where CaseID = (%d);", caseid);
	db.execDML(buf);
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  LineLdProblemList where CaseID = (%d);", caseid);
	db.execDML(buf);
	
	db.execDML("begin transaction;");

    BUSVector BusListc = inst->GetBusList();

	for (int i = 0; i < LineLdInfUncompletNum; i++)
	{
		char buf[400];
		sprintf_s(buf, "insert into LineLdInfUncompletList(CaseID ,LineID ,StartBus ,StartVol, EndBus ,EndVol ,LineR ,PowerDc ,VoltageDc ,CurrentDc ,LinePro,Linelength) values (%d,%d,'%s',%f,'%s',%f,%f,%f,%f,%f,'%s',%f);",
			caseid, i + 1, LineLdInfUncomplet[i].LineStart.c_str(), LineLdInfUncomplet[i].StartVol,
			LineLdInfUncomplet[i].LineEnd.c_str(), LineLdInfUncomplet[i].EndVol, LineLdInfUncomplet[i].Rdc, LineLdInfUncomplet[i].Pdc,
			LineLdInfUncomplet[i].Vdc, LineLdInfUncomplet[i].Idc, LineLdInfUncomplet[i].LinePro.c_str(), LineLdInfUncomplet[i].linelength);
		db.execDML(buf);
	}
	for (int i = 0; i < LineLdProblemNum; i++)
	{
		char buf[400];
		sprintf_s(buf, "insert into LineLdProblemList(CaseID ,LineID ,StartBus ,StartVol, EndBus ,EndVol ,LineR ,PowerDc ,VoltageDc ,CurrentDc ,LinePro,Linelength) values (%d,%d,'%s',%f,'%s',%f,%f,%f,%f,%f,'%s',%f);",
			caseid, LineLdProblem[i].LineID, LineLdProblem[i].LineStart.c_str(), LineLdProblem[i].StartVol,
			LineLdProblem[i].LineEnd.c_str(), LineLdProblem[i].EndVol, LineLdProblem[i].Rdc, LineLdProblem[i].Pdc,
			LineLdProblem[i].Vdc, LineLdProblem[i].Idc, LineLdProblem[i].LinePro.c_str(), LineLdProblem[i].linelength);
		db.execDML(buf);
	}
	char buf1[200];
	sprintf_s(buf1, "update LineLdInfUncompletList set LineR=NULL where LineR=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdInfUncompletList set PowerDc=NULL where PowerDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdInfUncompletList set VoltageDc=NULL where VoltageDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdInfUncompletList set CurrentDc=NULL where CurrentDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdInfUncompletList set Linelength=NULL where Linelength=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdProblemList set LineR=NULL where LineR=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdProblemList set PowerDc=NULL where PowerDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdProblemList set VoltageDc=NULL where VoltageDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdProblemList set CurrentDc=NULL where CurrentDc=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update LineLdProblemList set Linelength=NULL where Linelength=999999");
	db.execDML(buf1);
	db.execDML("commit transaction;");
	db.close();
	BusListc.clear();
}
//string LineLdJudgement::Unicode2Utf8(const std::wstring& widestring)  
//{  
//
//	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  
//	if (utf8size == 0)  
//	{  
//		throw std::exception("Error in conversion.");  
//	}  
//
//	std::vector<char> resultstring(utf8size);  
//
//	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);  
//
//	if (convresult != utf8size)  
//	{  
//		throw std::exception("La falla!");  
//	}  
//
//	return std::string(&resultstring[0]);  
//}  
//bool LineLdJudgement::StringToWString(const std::string &str,std::wstring &wstr)
//{
//	int nLen = (int)str.length();    
//	wstr.resize(nLen,L' ');
//
//	int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
//
//	if (nResult == 0)
//	{
//		return FALSE;
//	}
//
//	return TRUE;
//
//}
//wstring LineLdJudgement::str2Wstr(string str)
//{
//	wstring Wstr;
//	StringToWString(str,Wstr);
//	return Wstr;
//
//}
